package com.typhoon.spring_shiro.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.*;
import java.util.Properties;

/**
 * Created by hujun on 2017/2/15.
 */
public class CrmDataSourceUtils {

    public static Properties p       = null;
    public static String     confile = "jdbc-common.properties";

    static {
        p = new Properties();
        InputStream inputStream = null;
        try {
            //java应用
           /* confile = CrmDataSourceUtils.class.getClassLoader().getResource("/").getPath()
                    + confile;*/
            inputStream = CrmDataSourceUtils.class.getClassLoader().getResourceAsStream("jdbc-common.properties");
           /* System.out.println(confile);
            File file = new File(confile);
            inputStream = new BufferedInputStream(new FileInputStream(file));*/
            inputStream = new BufferedInputStream(inputStream);
            p.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static DataSource getDataSource() {
        try {
            return DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JdbcTemplate getJdbcTemplate() {
        DataSource   dataSource   = getDataSource();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }

    public static void main(String[] args) {
        getJdbcTemplate();
    }
}
